package org.yys.demo;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.yys.constant.MqConstant;
import org.yys.domain.MsgDomain;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class OrderlyDemo {

    private List<MsgDomain> msgList= Arrays.asList(
            new MsgDomain("order1",1L,"下单"),
            new MsgDomain("order1",1L,"支付"),
            new MsgDomain("order1",1L,"发货"),
            new MsgDomain("order2",1L,"下单"),
            new MsgDomain("order2",1L,"支付"),
            new MsgDomain("order2",1L,"发货")
    );


    public void orderlyProducer() throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        // 创建生产者
        DefaultMQProducer producer = new DefaultMQProducer("orderly-producer-group");
        producer.setNamesrvAddr(MqConstant.NAMESRV_ADDR);
        producer.start();

        for (MsgDomain msg : msgList) {
            Message message = new Message("orderlyTopic", msg.toString().getBytes());
            producer.send(message, new MessageQueueSelector() {
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    // 根据orderSn的hash对queue取余结果, 选择对应的queue. 以此保证相同orderSn的message都会进入相同的queue
                    int hashCode = o.toString().hashCode();
                    int i = Math.abs(hashCode % list.size());
                    return list.get(i);
                }
            },msg.getOrderSn());
            System.out.println("成功发送消息:"+msg);
        }

        producer.shutdown();

    }

    public void orderlyConsumer() throws MQClientException, InterruptedException, IOException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("orderly-consumer-group");
        consumer.setNamesrvAddr(MqConstant.NAMESRV_ADDR);
        consumer.subscribe("orderlyTopic","*");
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                long id = Thread.currentThread().getId();
                System.out.println("当前线程:"+id);
                byte[] body = list.get(0).getBody();
                System.out.println("接收到消息:"+ new String(body));
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });

        consumer.start();


        System.in.read();

    }

}
